package org.hackystat.ui.projectviewer.gwt.client.panels;

import org.hackystat.ui.projectviewer.gwt.client.common.MainPanelWidget;
import org.hackystat.ui.projectviewer.gwt.client.common.types.ProjectData;
import org.hackystat.ui.projectviewer.gwt.client.dataprovider.DataProviderMediator;
import org.hackystat.ui.projectviewer.gwt.client.dynatable.DynaTableWidget;
import org.hackystat.ui.projectviewer.gwt.client.widgets.PanelTitleWidget;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.VerticalPanel;

/**
 * Displays project info.
 *
 * @author Pavel Senin.
 *
 */
public class ProjectInfoPanel extends MainPanelWidget {

  /* the outer frame */
  private final VerticalPanel outer = new VerticalPanel();

  private final VerticalPanel inner = new VerticalPanel();

  private final PanelTitleWidget projectTitle = new PanelTitleWidget();

  private final HTML infoHTML = new HTML();

  private final PanelTitleWidget dataTitle = new PanelTitleWidget();

  /* dyna table widget */
  private final DynaTableWidget dynaTable;

  /**
   * Creates an instance and reports a handler.
   *
   * @return the widget handler.
   */
  public static MPHandler init() {
    return new MPHandler("projectInfo") {
      public MainPanelWidget createInstance() {
        return new ProjectInfoPanel();
      }
    };
  }

  /**
   * Instantiates the panel.
   *
   */
  public ProjectInfoPanel() {

    inner.setWidth("100%");
    projectTitle.setSingleTitle("Info panel");
    inner.add(projectTitle);

    inner.add(infoHTML);

    dataTitle.setSingleTitle("Latest project data");
    inner.add(dataTitle);

    String[] columns = new String[] { "SDT type", "Tool", "Owner", "Modified" };
    String[] styles = new String[] { "type", "tool", "owner", "modified" };
    dynaTable = new DynaTableWidget(columns, styles, 10);

    inner.add(dynaTable);

    outer.setStyleName("infoPanel");
    outer.add(inner);

    initWidget(outer);
  }

  /**
   * Set the info for the project specified.
   *
   * @param projectId The project name.
   */
  public void setProject(final String projectId) {
    projectTitle.setWait(true);
    projectTitle.setMainTitle(projectId);
    projectTitle.setSubTitle("loading data ...");
    infoHTML.setHTML("<p>getting data...</p>");
    dynaTable.clean();

    if (DataProviderMediator.getInstance().lock()) {
      // Setup the callback from the service.
      AsyncCallback<ProjectData> callback = new AsyncCallback<ProjectData>() {
        // If the call was successful, we will get back the result of authorization.
        public void onSuccess(ProjectData s) {
          DataProviderMediator.getInstance().unlock();
          ProjectData prj = (ProjectData) s;
          projectTitle.setWait(false);
          projectTitle.setSingleTitle(prj.getName());
          infoHTML.setHTML(prj.toHTML());
          // get last sensorData
          GWT.log("Done with project data, firing dynatable", null);
          dynaTable.setProjectName(projectId, prj.getOwner());
          dataTitle.setMainTitle(prj.getName() + " project sensor data:");
          dataTitle.setSubTitle("mini-browser");
          dynaTable.refresh();
        }

        // Something went wrong with the call. Handle the issue how you'd like.
        public void onFailure(Throwable ex) {
          DataProviderMediator.getInstance().unlock();
          Window.alert("Unknown exception happend during RPC call. \n Please provide feedback "
              + "to hackystat-dev@googlegroups.com. \n Error: " + ex.getMessage());
        }
      };
      DataProviderMediator.getInstance().getProjectData(projectId, callback);
    }
    else {
      Window.alert("RPC is locked, unable to proceed with communications. \n Please provide "
          + "feedback to hackystat-dev@googlegroups.com.");
    }
  }
}
